home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gdevvglb.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  10.0 KB  |  376 lines

  1. /* Copyright (C) 1992, 1993, 1994, 1996, 1997, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gdevvglb.c,v 1.3 2000/09/19 19:00:23 lpd Exp $ */
  20. /*
  21.  * This is a driver for 386 PCs using vgalib for graphics on the console
  22.  * display.  Note that this driver only works with 16-color modes.
  23.  *
  24.  * Written by Sigfrid Lundberg, siglun@euler.teorekol.lu.se.
  25.  * Modified by Erik Talvola, talvola@gnu.ai.mit.edu
  26.  * Updated 9/28/96 by L. Peter Deutsch, ghost@aladdin.com: allow setting
  27.  *   the display mode as a device parameter.
  28.  * Updated 2/13/97 by ghost@aladdin.com: make the device identify itself
  29.  *   as a page device.
  30.  * Updated 5/2/97 by ghost@aladdin.com: copy_mono computed some parameters
  31.  *   before doing fit_copy.
  32.  * Update 1997-06-28 by ghost@aladdin.com: get_bits wasn't implemented.
  33.  */
  34.  
  35. #include "gx.h"
  36. #include "gserrors.h"
  37. #include "gsparam.h"
  38. #include "gxdevice.h"
  39. #include "gdevpccm.h"
  40.  
  41. #include <errno.h>
  42. #include <vga.h>
  43.  
  44. typedef struct gx_device_vgalib {
  45.     gx_device_common;
  46.     int display_mode;
  47. } gx_device_vgalib;
  48.  
  49. #define vga_dev ((gx_device_vgalib *)dev)
  50.  
  51. #define XDPI   60        /* to get a more-or-less square aspect ratio */
  52. #define YDPI   60
  53.  
  54. #ifndef A4            /*Letter size */
  55. #define YSIZE (20.0 * YDPI / 2.5)
  56. #define XSIZE (8.5 / 11)*YSIZE    /* 8.5 x 11 inch page, by default */
  57. #else /* A4 paper */
  58. #define XSIZE 8.27
  59. #define YSIZE 11.69
  60. #endif
  61.  
  62. private dev_proc_open_device(vgalib_open);
  63. private dev_proc_close_device(vgalib_close);
  64. private dev_proc_map_rgb_color(vgalib_map_rgb_color);
  65. private dev_proc_map_color_rgb(vgalib_map_color_rgb);
  66. private dev_proc_fill_rectangle(vgalib_fill_rectangle);
  67. private dev_proc_tile_rectangle(vgalib_tile_rectangle);
  68. private dev_proc_copy_mono(vgalib_copy_mono);
  69. private dev_proc_copy_color(vgalib_copy_color);
  70. private dev_proc_get_bits(vgalib_get_bits);
  71. private dev_proc_get_params(vgalib_get_params);
  72. private dev_proc_put_params(vgalib_put_params);
  73.  
  74. const gx_device_vgalib gs_vgalib_device =
  75. {
  76.     std_device_std_body(gx_device_vgalib, 0, "vgalib",
  77.             0, 0, 1, 1),
  78.     {vgalib_open,
  79.      NULL,            /* get_initial_matrix */
  80.      NULL,            /* sync_output */
  81.      NULL,            /* output_page */
  82.      vgalib_close,
  83.      vgalib_map_rgb_color,
  84.      vgalib_map_color_rgb,
  85.      vgalib_fill_rectangle,
  86.      vgalib_tile_rectangle,
  87.      vgalib_copy_mono,
  88.      vgalib_copy_color,
  89.      NULL,            /* draw_line (obsolete) */
  90.      vgalib_get_bits,
  91.      vgalib_get_params,
  92.      vgalib_put_params,
  93.      NULL,            /* map_cmyk_color */
  94.      NULL,            /* get_xfont_procs */
  95.      NULL,            /* get_xfont_device */
  96.      NULL,            /* map_rgb_alpha_color */
  97.      gx_page_device_get_page_device
  98.     },
  99.     -1                /* display_mode */
  100. };
  101.  
  102. private int
  103. vgalib_open(gx_device * dev)
  104. {
  105.     int VGAMODE = vga_dev->display_mode;
  106.     int width = dev->width, height = dev->height;
  107.  
  108.     if (VGAMODE == -1)
  109.     VGAMODE = vga_getdefaultmode();
  110.     if (VGAMODE == -1)
  111.     vga_setmode(G640x480x16);
  112.     else
  113.     vga_setmode(VGAMODE);
  114.     vga_clear();
  115.     if (width == 0)
  116.     width = vga_getxdim() + 1;
  117.     if (height == 0)
  118.     height = vga_getydim() + 1;
  119.  
  120.     /*vgalib provides no facilities for finding out aspect ratios */
  121.     if (dev->y_pixels_per_inch == 1) {
  122.     dev->y_pixels_per_inch = height / 11.0;
  123.     dev->x_pixels_per_inch = dev->y_pixels_per_inch;
  124.     }
  125.     gx_device_set_width_height(dev, width, height);
  126.  
  127.     /* Find out if the device supports color */
  128.     /* (default initialization is monochrome). */
  129.     /* We only recognize 16-color devices right now. */
  130.     if (vga_getcolors() > 1) {
  131.     int index;
  132.  
  133.     static const gx_device_color_info vgalib_16color = dci_pc_4bit;
  134.  
  135.     dev->color_info = vgalib_16color;
  136.  
  137.     for (index = 0; index < 16; ++index) {
  138.         gx_color_value rgb[3];
  139.  
  140.         (*dev_proc(dev, map_color_rgb)) (dev, (gx_color_index) index, rgb);
  141. #define cv2pv(cv) ((cv) >> (gx_color_value_bits - 8))
  142.         vga_setpalette(index, cv2pv(rgb[0]), cv2pv(rgb[1]), cv2pv(rgb[2]));
  143. #undef cv2pv
  144.     }
  145.     }
  146.     return 0;
  147. }
  148.  
  149. private int
  150. vgalib_close(gx_device * dev)
  151. {
  152.     vga_setmode(TEXT);
  153.     return 0;
  154. }
  155.  
  156. private gx_color_index
  157. vgalib_map_rgb_color(gx_device * dev, gx_color_value red,
  158.              gx_color_value green, gx_color_value blue)
  159. {
  160.     return pc_4bit_map_rgb_color(dev, red, green, blue);
  161. }
  162.  
  163. private int
  164. vgalib_map_color_rgb(gx_device * dev, gx_color_index index,
  165.              unsigned short rgb[3])
  166. {
  167.     return pc_4bit_map_color_rgb(dev, index, rgb);
  168. }
  169.  
  170. private int
  171. vgalib_tile_rectangle(gx_device * dev, const gx_tile_bitmap * tile,
  172.               int x, int y, int w, int h, gx_color_index czero,
  173.               gx_color_index cone, int px, int py)
  174. {
  175.     if (czero != gx_no_color_index && cone != gx_no_color_index) {
  176.     vgalib_fill_rectangle(dev, x, y, w, h, czero);
  177.     czero = gx_no_color_index;
  178.     }
  179.     return gx_default_tile_rectangle(dev, tile, x, y, w, h, czero, cone, px,
  180.                      py);
  181. }
  182.  
  183. private int
  184. vgalib_fill_rectangle(gx_device * dev, int x, int y, int w, int h,
  185.               gx_color_index color)
  186. {
  187.     int i, j;
  188.  
  189.     fit_fill(dev, x, y, w, h);
  190.     vga_setcolor((int)color);
  191.     if ((w | h) > 3) {        /* Draw larger rectangles as lines. */
  192.     if (w > h)
  193.         for (i = y; i < y + h; ++i)
  194.         vga_drawline(x, i, x + w - 1, i);
  195.     else
  196.         for (j = x; j < x + w; ++j)
  197.         vga_drawline(j, y, j, y + h - 1);
  198.     } else {            /* Draw small rectangles point-by-point. */
  199.     for (i = y; i < y + h; i++)
  200.         for (j = x; j < x + w; j++)
  201.         vga_drawpixel(j, i);
  202.     }
  203.     return 0;
  204. }
  205.  
  206. private int
  207. vgalib_copy_mono(gx_device * dev, const byte * base, int sourcex,
  208.          int raster, gx_bitmap_id id, int x, int y, int width,
  209.          int height, gx_color_index zero, gx_color_index one)
  210. {
  211.     const byte *ptr_line;
  212.     int left_bit, dest_y, end_x;
  213.     int invert = 0;
  214.     int color;
  215.  
  216.     fit_copy(dev, base, sourcex, raster, id, x, y, width, height);
  217.     ptr_line = base + (sourcex >> 3);
  218.     left_bit = 0x80 >> (sourcex & 7);
  219.     dest_y = y, end_x = x + width;
  220.  
  221.     if (zero == gx_no_color_index) {
  222.     if (one == gx_no_color_index)
  223.         return 0;
  224.     color = (int)one;
  225.     } else {
  226.     if (one == gx_no_color_index) {
  227.         color = (int)zero;
  228.         invert = -1;
  229.     } else {        /* Pre-clear the rectangle to zero */
  230.         vgalib_fill_rectangle(dev, x, y, width, height, zero);
  231.         color = (int)one;
  232.     }
  233.     }
  234.  
  235.     vga_setcolor(color);
  236.     while (height--) {        /* for each line */
  237.     const byte *ptr_source = ptr_line;
  238.     register int dest_x = x;
  239.     register int bit = left_bit;
  240.  
  241.     while (dest_x < end_x) {    /* for each bit in the line */
  242.         if ((*ptr_source ^ invert) & bit)
  243.         vga_drawpixel(dest_x, dest_y);
  244.         dest_x++;
  245.         if ((bit >>= 1) == 0)
  246.         bit = 0x80, ptr_source++;
  247.     }
  248.  
  249.     dest_y++;
  250.     ptr_line += raster;
  251.     }
  252.     return 0;
  253. }
  254.  
  255.  
  256. /* Copy a color pixel map.  This is just like a bitmap, except that */
  257. /* each pixel takes 4 bits instead of 1 when device driver has color. */
  258. private int
  259. vgalib_copy_color(gx_device * dev, const byte * base, int sourcex,
  260.           int raster, gx_bitmap_id id, int x, int y,
  261.           int width, int height)
  262. {
  263.  
  264.     fit_copy(dev, base, sourcex, raster, id, x, y, width, height);
  265.  
  266.     if (gx_device_has_color(dev)) {    /* color device, four bits per pixel */
  267.     const byte *line = base + (sourcex >> 1);
  268.     int dest_y = y, end_x = x + width;
  269.  
  270.     if (width <= 0)
  271.         return 0;
  272.     while (height--) {    /* for each line */
  273.         const byte *source = line;
  274.         register int dest_x = x;
  275.  
  276.         if (sourcex & 1) {    /* odd nibble first */
  277.         int color = *source++ & 0xf;
  278.  
  279.         vga_setcolor(color);
  280.         vga_drawpixel(dest_x, dest_y);
  281.         dest_x++;
  282.         }
  283.         /* Now do full bytes */
  284.         while (dest_x < end_x) {
  285.         int color = *source >> 4;
  286.  
  287.         vga_setcolor(color);
  288.         vga_drawpixel(dest_x, dest_y);
  289.         dest_x++;
  290.  
  291.         if (dest_x < end_x) {
  292.             color = *source++ & 0xf;
  293.             vga_setcolor(color);
  294.             vga_drawpixel(dest_x, dest_y);
  295.             dest_x++;
  296.         }
  297.         }
  298.  
  299.         dest_y++;
  300.         line += raster;
  301.     }
  302.     } else {            /* monochrome device: one bit per pixel */
  303.     /* bitmap is the same as bgi_copy_mono: one bit per pixel */
  304.     vgalib_copy_mono(dev, base, sourcex, raster, id, x, y, width, height,
  305.              (gx_color_index) 0, (gx_color_index) 7);
  306.     }
  307.  
  308.     return 0;
  309. }
  310.  
  311. /* Read bits back from the device. */
  312. private int
  313. vgalib_get_bits(gx_device * dev, int y, byte * data, byte ** actual_data)
  314. {
  315.     int x;
  316.     byte *dest = data;
  317.     int b = 0;
  318.     int depth = dev->color_info.depth;    /* 1 or 4 */
  319.     int mask = (1 << depth) - 1;
  320.     int left = 8;
  321.  
  322.     if (actual_data)
  323.     *actual_data = data;
  324.     for (x = 0; x < dev->width; ++x) {
  325.     int color = vga_getpixel(x, y);
  326.  
  327.     if ((left -= depth) < 0)
  328.         *dest++ = b, b = 0, left += 8;
  329.     b += (color & mask) << left;
  330.     }
  331.     if (left < 8)
  332.     *dest = b;
  333.     return 0;
  334. }
  335.  
  336. /* Get/put the display mode parameter. */
  337. private int
  338. vgalib_get_params(gx_device * dev, gs_param_list * plist)
  339. {
  340.     int code = gx_default_get_params(dev, plist);
  341.  
  342.     if (code < 0)
  343.     return code;
  344.     return param_write_int(plist, "DisplayMode", &vga_dev->display_mode);
  345. }
  346. private int
  347. vgalib_put_params(gx_device * dev, gs_param_list * plist)
  348. {
  349.     int ecode = 0;
  350.     int code;
  351.     int imode = vga_dev->display_mode;
  352.     const char *param_name;
  353.  
  354.     switch (code = param_read_int(plist, (param_name = "DisplayMode"), &imode)) {
  355.     default:
  356.         ecode = code;
  357.         param_signal_error(plist, param_name, ecode);
  358.     case 0:
  359.     case 1:
  360.         break;
  361.     }
  362.  
  363.     if (ecode < 0)
  364.     return ecode;
  365.     code = gx_default_put_params(dev, plist);
  366.     if (code < 0)
  367.     return code;
  368.  
  369.     if (imode != vga_dev->display_mode) {
  370.     if (dev->is_open)
  371.         gs_closedevice(dev);
  372.     vga_dev->display_mode = imode;
  373.     }
  374.     return 0;
  375. }
  376.